package com.lhkj.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

import com.lhkj.model.Email;
import com.lhkj.service.EmailService;
import com.lhkj.service.impl.EmailServiceImpl;
import com.lhkj.util.bean.MyAuthenticator;

public class EmailServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private static Properties proper = new Properties();
	static {
		proper = new Properties();
		InputStream is = MyAuthenticator.class.getResourceAsStream("/config.properties");
		try {
			proper.load(is);
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Logger emailLog = Logger.getLogger("payment");
		EmailService emailService = new EmailServiceImpl();
		Email mailInfo = getParameterToemailInfo(request, response);
		mailInfo.setMailServerHost(proper.getProperty("mailServerHost"));
		mailInfo.setMailServerPort(proper.getProperty("mailServerPost"));
		mailInfo.setValidate(true);
		mailInfo.setUserName(proper.getProperty("userName"));
		mailInfo.setPassword(proper.getProperty("password"));//邮箱密码
		mailInfo.setFromAddress(proper.getProperty("fromAddress"));
		// 目标邮箱
		mailInfo.setToAddress(proper.getProperty("toAddress"));

		boolean result = emailService.sendEmail(mailInfo);
		System.out.println(result + "------------");
		if(result) {
			emailLog.info("发送邮件成功:"  + "fromAddress【" + proper.getProperty("fromAddress") + "】" + "toAddress【" + proper.getProperty("toAddress") + "】");
			emailLog.info("邮件内容：【" + mailInfo.getContent()  + "】");
			request.setAttribute("info", "发送成功");
		}else {
			emailLog.info("发送邮件失败:"  + "fromAddress【" + proper.getProperty("fromAddress") + "】" + "toAddress【" + proper.getProperty("toAddress") + "】");
			emailLog.info("邮件内容：【" + mailInfo.getContent()  + "】");
			request.setAttribute("info", "发送失败");
		}
		request.getRequestDispatcher("cooperation.jsp").forward(request, response);
	}

	private Email getParameterToemailInfo(HttpServletRequest request,
			HttpServletResponse response) throws UnsupportedEncodingException {
		Email email = new Email();
		String titile = getString(request.getParameter("title"));// 标题
		String budget = request.getParameter("budget");// 预算
		String developmentTime = request.getParameter("development_time");// 开发时间
		String phone = request.getParameter("phone");// 联系电话
		String describe = request.getParameter("describe");// 项目描述
		describe =  new String(describe.getBytes("iso-8859-1"), "utf-8");
		email.setSubject("项目分类： " + titile);
		String textContent = "类别：" + titile + "\n";
		if (!"".equals(budget)) {
			textContent = textContent + "项目预算：" + budget.trim() + "\n";
		}
		if (!"".equals(developmentTime)) {
			textContent = textContent + "开发时间：" + developmentTime.trim() + "天" + "\n";
		}

		if (!"".equals(phone)) {
			textContent = textContent + "联系电话：" + phone.trim() + "\n";
		}

		if (!"".equals(describe)) {
			textContent = textContent + "项目描述：" + describe.trim() + "\n";
		}
		textContent = textContent + "发送时间：" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
		email.setContent(textContent);
		return email;
	}

	private String getString(String parameter) {
		int i = Integer.parseInt(parameter);
		String result = null;
		switch (i) {
		case 1:
			result = "整体解决方案";
			break;
		case 2:
			result = "Android开发";
			break;
		case 3:
			result = "ios开发";
			break;
		case 4:
			result = "WP8开发";
			break;
		case 5:
			result = "网站开发";
			break;
		case 6:
			result = "后台服务开发";
			break;
		case 7:
			result = "UI设计";
			break;
		case 8:
			result = "短期外包";
		}
		return result;
	}
}
